<template>
  <el-main>
    <el-form :model="searchForm" ref="form" label-width="80px" :inline="true" size="small">
      <el-form-item label="学院名称">
        <el-input v-model="searchForm.collegeName" placeholder="请输入学院名称"></el-input>
      </el-form-item>
      <el-form-item label="专业名称">
        <el-input v-model="searchForm.majorName" placeholder="请输入专业名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" plain @click="searchBtn">搜索</el-button>
        <el-button type="danger" plain icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button type="success" plain icon="el-icon-plus" @click="addBtn">新增学院</el-button>
      </el-form-item>
    </el-form>

    <sys-dialog
      :title="addDialog.title"
      :visible="addDialog.visible"
      :width="addDialog.width"
      :height="addDialog.height"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form :model="addModel" ref="addForm" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item prop="collegeId" label="所属学院">
            <el-select style="width: 100%;" v-model="addModel.collegeId" clearable placeholder="请选择">
              <el-option
                v-for="item in collegeList"
                :key="item.collegeId"
                :label="item.collegeName"
                :value="item.collegeId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业名称" prop="majorName">
            <el-input v-model="addModel.majorName"></el-input>
          </el-form-item>

          <el-form-item label="专业序号">
            <el-input v-model="addModel.orderNum"></el-input>
          </el-form-item>
        </el-form>

      </div>
    </sys-dialog>
  </el-main>
</template>

<script>
//这是弹窗组件
import SysDialog from '@/components/dialog/StstemDialog.vue'
import {addApi, editApi, getCollegeListApi} from '@/api/major'
import {addMenuApi, editMenuApi} from "@/api/menu";

export default {
  components: {
    SysDialog
  },
  data() {
    return {
      rules:{
        collegeId: [{trigger: 'blur', required: true, message: '请选择所属学院'}],
        majorName: [{trigger: 'blur', required: true, message: '请填写专业名称'}],
      },
      //表单绑定
      addModel:{
        type:'',
        majorId:'',
        collegeId:'',
        majorName:'',
        orderNum: ''
      },
      addDialog: {title: '', height: 200, width: 550, visible: false},
      searchForm: {
        majorName: '',
        collegeName: '',
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      collegeList: [],
    }
  },
  methods: {
    searchBtn() {

    },
    resetBtn() {
    },
    async addBtn() {
      this.$resetForm('addForm', this.addModel)
      this.addDialog.title="新增专业"
      this.addDialog.visible=true
      this.addModel.type= '0'
      //先获取学院列表
      let res = await getCollegeListApi()
      if (res && res.code == 200) {
        console.log(res)
        this.collegeList = res.data
      }

    },
    onClose(){
      this.addDialog.visible=false
    },
    onConfirm(){
      this.$refs.addForm.validate( async (valid) => {
        if (valid) {
          let res=null
          if (this.addModel.type=='0'){
             res = await addApi(this.addModel)
          }else {
            res = await editApi(this.addModel)
          }
          if (res && res.code == 200) {
            //信息提示
            this.$message({type: 'success', message: res.msg})
            this.dialog.visible = false;
          }
        }
      })
    },
  },

}
</script>

<style lang="scss" scoped>

</style>
